Page({

  /**
   * 页面的初始数据
   */
  data: {
    cid: '',
    name: '',
    classListPer:[],
    launch_len:0,
    selectList:[],
    allselect:false,
    isBj:false,
    addname:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      cid: options.cid,
      name: options.name,
    })
    console.log(this.data.cid);
  },

  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },
  checkboxChange:function(e){
    console.log(e.detail.value)
  },
  bj(){
    this.data.isBj=!this.data.isBj
    this.setData({
      isBj:this.data.isBj
    })
  },
  getmember(e){
    this.data.addname=e.detail.value
    
  },
  addmember:async function(){
    console.log(this.data.addname);
    await wx.User.addListMember({
      data:{
        listId:this.data.cid,
        name:this.data.addname
      }
    })
    this.data.addname=''
    this.setData({
      addname:this.data.addname
    })
    this.hideModal()
    this.onShow()
  },
  async upFile(){
    var cid=this.data.cid
    var that=this
    console.log(cid);
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success(res) {
        console.log(res)
        const path = res.tempFiles[0].path
        let size = res.tempFiles[0].size;
        let filename = res.tempFiles[0].name;
        wx.uploadFile({
          url: 'https://www.xqzjgsu.top:8001/list/insertMembers/excel', //仅为示例，非真实的接口地址
          filePath: path,
          name: 'list',
          header: {
            'content-type': 'multipart/form-data',
            'Authorization': 'Bearar ' + wx.getStorageSync('token')
          },
          formData: {
            listId:cid
           
          },
          success: function(res){
         
            console.log(res.data)
            var json = JSON.parse(res.data)
            if (json.code == 200) {
              wx.showToast({
                title: json.message,
                icon: "success"
              })
              that.hideModal()
              that.onShow()
            } else {
              wx.showToast({
                title: json.message,
                icon: 'none'
              })
              that.hideModal()
            }
           
            //do something
          }
        })
  
      }
      })
    
  },
  
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  action(e){
    var index=e.currentTarget.dataset.index
    console.log(e.currentTarget.dataset.index)
    if(this.data.classListPer[index].checked==false){
      this.data.classListPer[index].checked=true
    }
    else{
      this.data.classListPer[index].checked=false
    }
    var num=0
    console.log(this.data.classListPer)
    for(var i=0;i<this.data.launch_len;i++){
      if(this.data.classListPer[i].checked==true){
        num=num+1
       
      }
    }
    console.log(num);
    if(num==this.data.launch_len){
      this.data.allselect=true
    }else{
      this.data.allselect=false
    }

    this.data.selectList=[]
    this.setData({
      classListPer:this.data.classListPer,
      allselect:this.data.allselect
    })
    console.log(this.data.classListPer);
  },
  allselect(){
    this.data.allselect=!this.data.allselect
    console.log(this.data.allselect)
    if(this.data.allselect==true){
      for(var i=0;i<this.data.launch_len;i++){
        this.data.classListPer[i].checked=true
      }
    }else{
      for(var i=0;i<this.data.launch_len;i++){
        this.data.classListPer[i].checked=false
      }
    }
    this.data.selectList=[]
    this.setData({
      classListPer:this.data.classListPer,
      allselect:this.data.allselect
    })
    console.log(this.data.classListPer);
  },
  
  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  // 删除名单某成员
  deleteMember:async function(){
      for(var i=0;i<this.data.launch_len;i++){
        if(this.data.classListPer[i].checked==true){
          this.data.selectList.push(this.data.classListPer[i].name)
        }
      }
      console.log(this.data.selectList.join(','));
      await wx.User.deleteListMember({
        data:{
          listId: this.data.cid,
          names:this.data.selectList.join(',')
          
        }
      })
      this.data.isBj=false
      this.setData({
        isBj:this.data.isBj
      })
      this.onShow()

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:async function () {
    await wx.User.selectUserList({
      data:{
        listId:this.data.cid
      }
    })
    this.data.classListPer=wx.getStorageSync('classListPer')
    this.setData({
      classListPer:this.data.classListPer,
      launch_len:wx.getStorageSync('classListPer').length
    })
    console.log(this.data.classListPer);
    for(var i=0;i<this.data.launch_len;i++){
      this.data.classListPer[i].checked=false
    }
    console.log(this.data.classListPer);

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})